package org.crown.project.api.module.wechatuser.controller;

import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import com.github.pagehelper.PageInfo;
import org.crown.common.annotation.EncryptEnable;
import org.crown.common.annotation.NotLogin;
import org.crown.framework.responses.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.crown.project.api.module.wechatuser.domain.TbWechatUser;
import org.crown.project.api.module.wechatuser.service.TbWechatUserService;

import org.springframework.web.bind.annotation.RestController;
import org.crown.project.api.BaseApi;

import java.util.List;

/**
 * <p>
 * 功能：微信用户信息
 * </p>
 *
 * @author WuFengSheng
 * @since 2020-03-07
 */
@Slf4j
@Api(tags = "表名：tb_wechat_user 功能：微信用户信息")
@RestController
@RequestMapping(BaseApi.API_PATH_PREFIX + TbWechatUserApi.API_VERSION + "/tbWechatUser")
public class TbWechatUserApi extends BaseApi {

    /**
     * 接口版本
     */
    public static final String API_VERSION = "v1";

    @Autowired
    private TbWechatUserService tbWechatUserService;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("getById")
    @EncryptEnable
    public ResponseVo getById(@RequestParam("id") Long id){
        TbWechatUser tbWechatUser = tbWechatUserService.getById(id);
        log.info("tbWechatUser={}", tbWechatUser);
        if(tbWechatUser != null){
            return ResponseVo.success(tbWechatUser);
        }
        return ResponseVo.fail();
    }

    /**
     * 查询表表
     * @return
     */
    @GetMapping("list")
    @EncryptEnable
    public ResponseVo list(){
        Long userId = getUserId();
        log.info("userId={}", userId);
        QueryWrapper queryWrapper = new QueryWrapper();
        List<TbWechatUser> list = tbWechatUserService.list(queryWrapper);
        if(list != null){
            return ResponseVo.success(list);
        }
        return ResponseVo.fail();
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param tbWechatUser
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("page")
    @EncryptEnable
    public ResponseVo page(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize, @ApiParam("tbWechatUser") TbWechatUser tbWechatUser){
        Long userId = getUserId();
        log.info("userId={}, tbWechatUser={}", userId, tbWechatUser);
        PageInfo<TbWechatUser> page = tbWechatUserService.page(pageNum, pageSize, tbWechatUser);
        if(page != null){
            return ResponseVo.success(page);
        }
        return ResponseVo.fail();
    }

    /**
     * 保存或更新信息
     * @param tbWechatUser
     * @return
     */
    @ApiOperation("保存或更新信息")
    @PostMapping("saveOrUpdate")
    @EncryptEnable
    public ResponseVo saveOrUpdate(@ApiParam("tbWechatUser") TbWechatUser tbWechatUser){
        log.info("tbWechatUser={}", tbWechatUser);
        if(tbWechatUserService.saveOrUpdate(tbWechatUser)){
            return ResponseVo.success(tbWechatUser);
        }
        return ResponseVo.fail();
    }


}

